import 'package:dio/dio.dart';
import 'package:wk_weather/bean/NetRespondResult.dart';
import 'package:wk_weather/generated/json/mybean/CitySearchReasult.dart';

/// 高德地图 key
const String KEY_GAODE_flutter ="d74b9b12f49a2562efe0c234879265d9";
/// 根据 城市名字 获取城市信息
const String cityBaseUrl = "https://restapi.amap.com/v3/config/district?subdistrict=1&key=$KEY_GAODE_flutter&keywords=";
/// 根据 经纬度 获取城市信息
const String geoBaseUrl = "https://restapi.amap.com/v3/geocode/regeo?key=$KEY_GAODE_flutter&location=";
/// 彩云天气api
const String weatherBaseUrl = "https://api.caiyunapp.com/v2.5/OvEe8Zce5ASVrwB5/";

///网络超时
const NETWORK_TIMEOUT = -2;
/// Connection refused
const GITHUB_API_REFUSED = -4;
const REQUEST_SUCCESS = 200;

/// Dio http请求 工具类
class NetManager {
  static NetManager _instance = null;
  Dio _dio;

  NetManager(){
    print("NetManager-- 初始化");
    _dio = Dio (BaseOptions(
      baseUrl: weatherBaseUrl,
      connectTimeout: 15000, // 链接超时
      receiveTimeout: 15000, //
      sendTimeout: 15000, // 发送超时
    ));

    _dio.interceptors.add(LogInterceptor()); // 日志拦截器
  }


  static NetManager getInstance() {
    if(_instance==null){
      _instance = NetManager() ;
    }
    return _instance;
  }

  baseUrl(String baseUrl) {
    _dio.options.baseUrl = baseUrl;
    return _instance;
  }

  Future<RepResult> get(String url, {Map<String, dynamic> header, Map<String, dynamic> param} ) async {
    Response response;
    try {
      response = await _dio.get(url, queryParameters: param);
    } on DioError catch (e) {
      return resultError(e);
    }
    if (response.data is DioError) {
      return resultError(response.data);
    }

    return new RepResult(response.data, true );
  }

  resultError( DioError e) {
    Response errorResponse;
    if (e.response != null) {
      errorResponse = e.response;
    } else {
      errorResponse = Response(statusCode: 678);
    }
    if (e.type == DioErrorType.CONNECT_TIMEOUT || e.type == DioErrorType.RECEIVE_TIMEOUT) {
      errorResponse.statusCode = NETWORK_TIMEOUT;
    }
    return RepResult(e.message,  false );
  }


  /// 根据经纬度 从彩云api 获得天气数据
  Future<dynamic> loadWeatherData(String longitude, String latitude) async {
     //   https://api.caiyunapp.com/v2.5/sas9gfwyRX2NVehl/119.452753,32.204402/weather.json
    RepResult res = await  baseUrl(weatherBaseUrl).get("$longitude,$latitude/weather.json");
    if (res != null && res.respondStatus) {
      return res.respondData;
    }
    return null;
  }

  /// 根据城市名字 从高德api 获得 城市经纬度等信息
  Future<DistrictModelEntity> searchCity(String keywords) async {
    RepResult res = await  baseUrl(cityBaseUrl).get("$keywords");
    if (res != null && res.respondStatus) {
      return DistrictModelEntity().fromJson(res.respondData);
    }
    return null;
  }

  /// 根据 经纬度 获得地址详情
  Future<dynamic> reGeo(String location) async {
    //  https://restapi.amap.com/v3/geocode/regeo?key=d74b9b12f49a2562efe0c234879265d9&location=119.452753,32.204402
    RepResult res = await baseUrl(geoBaseUrl).get("$location");
    if (res != null && res.respondStatus) {
      return res.respondData;
    }
    return null;
  }
}


